package cheme.ProcessModel.Interpreter;

import java.util.Iterator;

import cheme.ProcessModel.AbstractParent;
import cheme.ProcessModel.IModelElement;
import cheme.ProcessModel.ModelEnv;
import cheme.ProcessModel.Equations.Equation;
import cheme.ProcessModel.Interpreter.IRule.RuleResult;
import cheme.ProcessModel.Table.Field;



public abstract class IterateEquations implements IRule
{
	protected Interpreter interpreter = null;
	private Equation equation = null;

	protected abstract RuleResult tokenCheck(Field equationToken);
	
	public RuleResult applyTo(IModelElement object)
	{
		RuleResult result = null;
		if(object instanceof Equation)
		{
			setEquation((Equation)object);
			interpreter = ModelEnv.getInstanceFor(getEquation()).getInterpreter();
			result = new IRule.Success((IModelElement)this.getEquation());
			
			Iterator<Field> iterator =  getEquation().iterator();
			Field field = null;			
			while(iterator.hasNext() && (result instanceof IRule.Success)) 
			{
				field = iterator.next();
				result = tokenCheck(field);
				
			}
		}
		return result;
	}

	public void setEquation(Equation equation)
	{
		this.equation = equation;
	}
	
	public Equation getEquation()
	{
		return equation;
	}

}
